package concurrency;

/**
 * Created by sunzhengjie on 2017/1/17.
 */
public class TestJoin {

    static class Sleeper extends Thread {
        private int duration;

        public Sleeper(String name, int sleepTime) {
            super(name);
            duration = sleepTime;
            start();
        }

        @Override
        public void run() {
            try {
                sleep(duration);
            } catch (InterruptedException ex) {
                System.out.println(getName() + " was interrupted." +
                        " isInterrupted:" + isInterrupted());
            }
            System.out.println(getName() + "has awakened");
        }
    }

    static class Joiner extends Thread {
        private Sleeper sleeper;

        public Joiner(String name, Sleeper sleeper) {
            super(name);
            this.sleeper = sleeper;
            start();
        }

        public void run() {
            try {
                sleeper.join();
            } catch (InterruptedException e) {
                System.out.println("Interrupted");
            }
            System.out.println(getName() + " join completed");
        }
    }

    public static class Joining {
        public static void main(String[] arg9) {
            Sleeper
                    sleeper = new Sleeper("sleepy", 1500),
                    grumpy = new Sleeper("grumpy", 1500);
            Joiner
                    dopey = new Joiner("Dopey", sleeper),
                    doc = new Joiner("doc", grumpy);
            grumpy.interrupt();
        }
    }
}
